<?php

class Default_ArticuloController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $postAlias = $this->_getParam('alias');

        $postModel = new Nosql_Model_Mysql_Post();
        $commentModel = new Nosql_Model_Mysql_Comment();
        $post = $postModel->getPostByAlias($postAlias);
        if ($post) {
            $this->view->post = $post;
            $this->view->title = $post['title'];
        } else {
            $this->view->title = "Articulo no encontrado";
            return;
        }

        // FORM COMMENTS
        $postUrl = "/articulo/index/alias/" . $postAlias . "/";
        $commentsForm = new Default_Form_Comment();

        // COMMENTS
        $this->view->commentsForm = $commentsForm->addComment($postUrl);
        $this->view->commentsForm = $this->processPostComment($this->view->commentsForm, $post['postid']);

        //get the comments
        $comments = $commentModel->listCommentsForPost($post['postid']);
        if ($comments){
            $this->view->comments = $comments;
        }


    }

    /**
     *
     * @param Default_Form_Comment $form
     * @param <type> $postid
     * @return <type>
     */
    private function processPostComment($form, $postid) {
        //Validate the form
        $request = $this->getRequest();
        $errors = array();
        $success = array();

        if ($request->isPost()) {
            //valiate if theh form is valid

            if ($form->isValid($request->getPost())) {

                //validate if the user is logged in
                $user = null;
                if (Zend_Auth::getInstance()->hasIdentity()) {
                    $user = Zend_Auth::getInstance()->getIdentity();
                }

                if ($user != null){
                    $formValues = $form->getValues();
                    $comment = $formValues['user']['comment'];

                    $commentModel = new Nosql_Model_Mysql_Comment();
                    $data['userid'] = $user->userid;
                    $data['postid'] = $postid;
                    $data['comment'] = $comment;
                    $response = $commentModel->createComment($data);

                    if ($response['status'] < 0){
                        $errorMsg = 'No se pudo agregar el comenario ';
                        if ('development' == APPLICATION_ENV){
                            $errorMsg .= $response['message'];
                        }
                        $errors[] = $errorMsg;
                        
                        $response['message'];
                    }else{
                        $form->getElement('comment')->setValue('');
                        $success[] = 'Su comentario a sido agregado';
                    }
                    
                }else{
                    //set an error message
                    $errors[] = 'Debe iniciar sesion para poder comentar';
                }
            }
        }

        $this->view->errors = $errors;
        $this->view->success = $success;
        return $form;
    }

}